<template>
  <div class="login-page">
    <!-- 头部 -->

    <van-nav-bar title="干货满满" />

    <!-- 表单 -->

    <van-form @submit="onSubmit">
      <!-- input -->
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="请输入用户名"
        :rules="[
          {required:true,message:'用户名不能为空'},
          // 正则表达式 检验格式  [] {} . * W w ......
          // ^abc  --->  abcabdkfhdkfjh
          // abc$  --->  3748yf7y4r47abc
          // ^abc[0-9]$  --->  abc7
          // w word >= 5
          //  \w
          {pattern:/^[\u4e00-\u9fa5a-zA-Z0-9.]{5,}$/ , message:'用户名至少包含5个字符'}
        ]"
      />

      <van-field
        v-model="password"
        name="password"
        type="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[
          {required:true,message:'密码不能为空'},
          {pattern:/^\w{7,}$/ , message:'密码长度错误!'}
        ]"
      />

      <div style="margin: 20px;">
        <van-button block type="info" navtive-type="submit">提交</van-button>
      </div>
    </van-form>

    <!-- 没有账号去注册 -->
    <router-link class="link" to="/register">没有账号 , 去注册</router-link>
  </div>
</template>

<script>
import { login } from '../api/user'

export default {
  name: 'LoginPage',
  data () {
    return {
      username: 'darkTchr',
      password: '123456789'
    }
  },

  methods: {
    // 监听表单的提交 (参数位置能拿到 表单的值)
    async  onSubmit (values) {
      const { data } = await login(values)

      console.log('data:', data)

      // 登陆成功啦
      this.$toast.success('okk~')
      // 做一下本地存储  // token = g7fh48rou9fed84dj834th74yt7t
      localStorage.setItem('vant-mobile-exp-token', data.token)
      this.$router.push('/')
    }
  }
}
</script>

<style lang="less">
  .link{
    color: #069;
    font-size: 12px;
    padding-right: 20px;
    float: right;
  }
</style>
